﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Enterprise.Management.ViewModels;
using Enterprise.Management.FilterAttributes;
using Enterprise.Management.Common;
using Enterprise.Management.Data;
using Enterprise.Management.Caches;
using System.IO;
using Enterprise.Management.Business;
using Enterprise.Management.Gridview;
using Enterprise.Management.DataTemplates;

namespace Enterprise.Management.Controllers
{
    [CategoryFunction]
    public class ContractController : BaseController
    {
        ContractBLL bllContract = new ContractBLL();

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(GridviewPage page)
        {
            var gridview = new Gridview.Gridview(page);

            gridview.Header.Add("网点", "DP_Category.Category_Name");
            gridview.Header.Add("合同", "LCInf_FileName");
            gridview.Header.Add("到期日", "LCInf_ExpiredDate");
            gridview.Header.Add(string.Empty);
            gridview.Header.Last.SetProperty("style", "width:80px");
            gridview.Header.Last.RowDataTemplate = new FilePathDataTemplate("_FilePathDataTemplatePartial")
            {
                FileName = "LCInf_FileName",
                FilePath = "LCInf_FilePath"
            };

            DeptPortalsDataContext context = new DeptPortalsDataContext();
            var query = from u in context.DP_License_Contract_Info
                        where u.LCInf_CtgrID == BranchCategoryCache.BranchCategoryId.Value
                        && u.LCInf_TypeID == (int)LicenseOrContract.Contract
                        && u.LCInf_Status != (int)StatusType.Deleted
                        select u;
            var data = IntelligentQueryBuilder.Create<DP_License_Contract_Info>(query, gridview.Page);

            gridview.Fill(data);

            return PartialView("_GridviewPartial", gridview);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(ContractViewModel vmContract)
        {
            if (ModelState.IsValid)
            {
                foreach (string upload in Request.Files)
                {
                    if (!Request.Files[upload].HasFile()) continue;
                    string path = FilePathGeneration.generate((int)FunctionType.ContractManagement, BranchCategoryCache.BranchCategoryId.Value);
                    string filename = Path.GetFileName(Request.Files[upload].FileName);

                    DP_License_Contract_Info dp_contract_info = new DP_License_Contract_Info();
                    dp_contract_info.LCInf_CtgrID = BranchCategoryCache.BranchCategoryId.Value;
                    dp_contract_info.LCInf_FileName = filename;
                    dp_contract_info.LCInf_FilePath = "../Resources/" + FilePathGeneration.getRefPath(path) + "/" + filename;
                    dp_contract_info.LCInf_TypeID = (int)LicenseOrContract.Contract;
                    dp_contract_info.LCInf_SubTypeID = vmContract.LCInf_SubTypeID;
                    dp_contract_info.LCInf_ExpiredDate = vmContract.LCInf_ExpiredDate;
                    dp_contract_info.LCInf_Status = (int)StatusType.Valid;
                    dp_contract_info.ModifiedBy = HtmlCache.GetUser().User_ID;
                    dp_contract_info.ModifiedDate = DateTime.Now;

                    if (bllContract.Add(dp_contract_info))
                    {
                        Request.Files[upload].SaveAs(Path.Combine(path, filename));
                        return RedirectToAction("Index");
                    }
                    else
                    {
                    }
                }
            }

            return View(vmContract);
        }
    }
}
